package ua.com.infopulse.activities;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import java.util.Date;

import ua.com.infopulse.Actions;
import ua.com.infopulse.Config;
import ua.com.infopulse.R;
import ua.com.infopulse.db.DBGetter;
import ua.com.infopulse.db.DataTrackManager;
import ua.com.infopulse.sevices.GpsUpdater;

public class CheckInWindow extends Activity {
	static String TAG = CheckInWindow.class.getSimpleName();
	private BroadcastReceiver receiver;
	public static final IntentFilter filter = new IntentFilter();


	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		new DBGetter(getApplication());  //init DB
		initButtons();
		regBroadcastReceiver();
	}

	private void regBroadcastReceiver() {
		filter.addAction(Actions.GPS_POSITION_ADDED);
		filter.addAction(Actions.GPS_POSITION_CHANGED);
		filter.addAction(Actions.GPS_UPDATER_STARTED);
		this.receiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				CheckInWindow.this.onBroadcast(intent);
			}
		};
	}

	private void onBroadcast(Intent intent) {
		String action = intent.getAction();
		if (Actions.GPS_POSITION_ADDED.equals(action)) {
			Toast.makeText(getApplication(), "GPS position ADDED", Toast.LENGTH_SHORT).show();
		} else if (Actions.GPS_POSITION_CHANGED.equals(action)) {
			Toast.makeText(getApplication(), "GPS position CHANGED", Toast.LENGTH_SHORT).show();
		} else if (Actions.GPS_UPDATER_STARTED.equals(action)) {
			Toast.makeText(getApplication(), "GPS updater STARTED", Toast.LENGTH_SHORT).show();
		}
	}


	private void initButtons() {
		Button check_button = (Button) findViewById(R.id.check_in_button);
		check_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				//add check in to db storage
				Log.d(TAG, "Start fix location");
				final LocationManager locationService = (LocationManager) CheckInWindow.this.getSystemService(Context.LOCATION_SERVICE);
				Location lastKnownLocation = locationService.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
				CheckInWindow.log_location(lastKnownLocation);
				EditText descriptionText = (EditText) findViewById(R.id.check_in_description);
				String desc = descriptionText.getText().toString();
				if (desc.trim().length() == 0) {
					desc = new Date(System.currentTimeMillis()).toString();
				}
				DataTrackManager manager = DBGetter.getDTM();
				try {
					manager.addCheckIN(lastKnownLocation, desc);
				} catch (IllegalArgumentException e) {
					Toast.makeText(getApplication(), "Can not get location", Toast.LENGTH_SHORT);
				}
				descriptionText.setText("");
				Toast.makeText(getApplication(), "Check in added", Toast.LENGTH_LONG).show();
			}
		});


		Button showCheckInList = (Button) findViewById(R.id.check_in_button_list);
		showCheckInList.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Log.d(TAG, "Show list button pressed");
				startActivity(new Intent(getApplication(), PointList.class));

			}
		});

		Button startGps = (Button) findViewById(R.id.start_gps_tracking);
		startGps.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent gpsTracker = new Intent(getApplication(), GpsUpdater.class);
				if (Config.isGPSEnabled) {
					Toast.makeText(getApplication(), "GPS tracking already active", Toast.LENGTH_LONG).show();
				} else {
					Config.isGPSEnabled = true;
					startService(gpsTracker);
				}
			}
		});
		Button stopGps = (Button) findViewById(R.id.stop_gps_tracking);

		stopGps.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Config.isGPSEnabled = false;
			}
		});

	}

	@Override
	protected void onStart() {
		super.onStart();
		registerReceiver(receiver, filter);

	}


	@Override
	protected void onStop() {
		super.onStop();
		registerReceiver(receiver, filter);
	}

	public static void log_location(Location location) {
		if (location != null) {
			double altitude = location.getAltitude();
			double longitude = location.getLongitude();
			Log.d(TAG, String.valueOf(altitude) + ":" + String.valueOf(longitude) + " _ " + location);
		} else {
			Log.d(TAG, "NULL pointer exception");
		}
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onCreatePanelMenu(int featureId, Menu menu) {

		return super.onCreatePanelMenu(featureId, menu);
	}
}
